using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Cleverscape.UTorrentClient.WebClient;

namespace uTorrentNetClient.ModelDesign.Converter
{
    [ValueConversion(typeof(OptionValue), typeof(bool))]
    public class OptionValueBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(Nullable<bool>))
            {
                return null;
            }
            if (value is OptionValue)
            {
                var ValueToConvert = (OptionValue)value;
                switch (ValueToConvert)
                {
                    case OptionValue.NotAllowed:
                        return false;
                    case OptionValue.Disabled:
                        return false;
                    case OptionValue.Enabled:
                        return true;
                    default:
                        break;
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(OptionValue))
            {
                return OptionValue.NotAllowed;
            }
            if (value is bool)
            {
                bool v = (bool)value;
                if (v)
                    return OptionValue.Enabled;
                else
                    return OptionValue.Disabled;
            }
            return OptionValue.NotAllowed;
        }
    }
}
